package cn.edu.Day06;

import cn.edu.Day06.Entity.Bullet;
import cn.edu.Day06.Entity.Enemy;
import cn.edu.Day06.Entity.Plane;
import cn.edu.Day06.Entity.Star;
import cn.edu.Day06.service.BulletService;
import cn.edu.Day06.service.EnemyService;
import cn.edu.Day06.service.Impl.BulletServiceImpl;
import cn.edu.Day06.service.Impl.EnemyServiceImpl;
import cn.edu.Day06.service.Impl.PlaneServiceImpl;
import cn.edu.Day06.service.Impl.StarServiceImpl;
import cn.edu.Day06.service.PlaneService;
import cn.edu.Day06.service.StarService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GamePanel extends JPanel implements Runnable{
    EnemyService enemyService;
    StarService starService;
    PlaneService planeService;
    BulletService bulletService;
    Star stars[];
    Random ran;
    JButton jtn;
    boolean isStart;
    Plane plane;
    boolean isCreateBullet;
    List<Bullet> bullets ;
    List<Enemy> enemies;

    public GamePanel()
    {
        enemies=new ArrayList<>();
        enemyService=new EnemyServiceImpl();
        bulletService=new BulletServiceImpl();
        bullets=new ArrayList<Bullet>();
        planeService = new PlaneServiceImpl();
        plane=planeService.createPlane();
        jtn=new JButton("开始游戏");
        jtn.setSize(100,30);
        jtn.setLocation(100,300);
        jtn.addActionListener((Event)->{
            GamePanel.this.remove(jtn);
            isStart=true;
        });
//        jtn.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                System.out.println("test");
//            }
//        });
        this.setLayout(null);
        this.add(jtn);
        ran=new Random();
        stars=new Star[300];
        starService=new StarServiceImpl();
        starService.createStars(stars,ran);
        new Thread(this).start();
        this.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent mouseEvent) {
                planeService.move(plane,mouseEvent);
                repaint();
            }
            @Override
            public void mouseMoved(MouseEvent mouseEvent) {
                planeService.move(plane,mouseEvent);
                repaint();
            }
        });

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                isCreateBullet=true;
            }
            public void mouseReleased(MouseEvent e)
            {
                isCreateBullet=false;
            }
        });
    }
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
       this.setBackground(Color.BLACK);
       starService.drawStar(g,stars);
       if(isStart)
       {
           //画其他东西
           //画飞机
           g.drawImage(Plane.IMG,plane.getX(),plane.getY(), plane.getWidth(),plane.getHeight(),null);
//           画自己的子弹
           bulletService.drawBullets(g,bullets);
//           画敌机
           enemyService.drawEnemies(g,enemies);
       }
    }
    @Override
    public void run() {
        int count=0;
        while (true)
        {
//            System.out.println("自己的子弹有"+bullets.size());
            if(count%15==0&&isStart)
            {
                enemyService.createEmemies(enemies,ran);
            }
            enemyService.down(enemies);

            count++;
            bulletService.up(bullets);
            if(isCreateBullet)
            {
//                System.out.println();
                if(count%3==0)
                {
                    bulletService.createBullet(bullets,plane);
                }
            }

            starsDown(stars);
            repaint();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    private void starsDown(Star[] stars)
    {
        for (int i = 0; i <stars.length ; i++)
        {
            Star star=stars[i];
            starService.starDown(star);

        }
    }
//    private void drawStar(Graphics g,Star[] stars) {
//        for (int i = 0; i <stars.length ; i++) {
//            Star star=stars[i];
//            g.setColor(star.getColor());
//            g.drawString(Star.NAME,star.getX(),star.getY());
//        }
//
//    }
//
//    private void createStars(Star[] stars) {
//        for (int i = 0; i <stars.length ; i++) {
//           Star star= stars[i]=new Star();
//           star.setX(ran.nextInt(GameFrame.W));
//           star.setY(ran.nextInt(GameFrame.H));
//           int red=ran.nextInt(256);
//           int green=ran.nextInt(256);
//           int blue=ran.nextInt(256);
//           star.setColor(new Color(red,green,blue));
//           star.setSpeed(3);
//        }
//    }
}
